home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / ctopas.cq / c-to-pas.c
Text File  |  1985-06-03  |  3KB  |  144 lines

  1. /* C to Pascal program -- filter to replace C punctuation and certain
  2.    key words with their Pascal equivalents.
  3.  
  4.   C form          Pascal form
  5.  -======-        -===========-
  6.     "                 '
  7.     {               BEGIN
  8.     }                END
  9.   <tab>        < 2 blank spaces >
  10.     ()            < nothing >
  11.     &&               AND
  12.     ||                OR
  13. comment start        {
  14. comment end          }
  15.     ==               =
  16.     !=               <>
  17.     =                :=
  18.  printf            writeln
  19.  scanf             readln
  20.  while             WHILE
  21.  
  22. Usage:  ctopas <infile >outfile
  23.  
  24. Copyright 1984 Ted Carnevale.
  25. Permission granted for personal nonprofit use.
  26. All other rights reserved.
  27. */
  28.  
  29. #include "stdio.h"
  30. #include "ctype.h"
  31. #define EOF -1
  32. #define EOS '\0'
  33.  
  34. main()
  35. {
  36.    char c, *letter, word[100];
  37.    int wordlnth;
  38.  
  39.    letter=word;
  40.    wordlnth=0;
  41.    while((c=getchar()) != EOF) {
  42.       if(isalpha(c)) letter[wordlnth++]=c;
  43.       else {
  44.          if(wordlnth>0) {            /* word ready to check */
  45.             letter[wordlnth]='\0';
  46.             wtest(word);             /* pass or replace it */
  47.             wordlnth=0;              /* reset index */
  48.          }
  49.          ctest(c);                   /* process following character */
  50.       }
  51.    }
  52. }       /* Note:  the last word in the file will be missed if it is
  53. immediately followed by EOF with no intervening nonalpha characters.
  54. This is not a problem for Pascal or C source files.  However,
  55. a general purpose workd filter would have to check for a nonzero
  56. wordlength after EOF is reached.   */
  57.  
  58. wtest(word)
  59. char *word;
  60. {
  61.    char *swapword;
  62.  
  63.    swapword=word;
  64.    switch(word[0]) {       /* test first letter, then rest of word */
  65.       case 'p':  if(strcmp(word,"printf\0")==0) swapword="writeln\0";
  66.         break;
  67.       case 's':  if(strcmp(word,"scanf\0")==0) swapword="readln\0";
  68.         break;
  69.       case 'w':  if(strcmp(word,"while\0")==0) swapword="WHILE\0";
  70.         break;
  71.       default:  break;       /* pass unchanged */
  72.    }
  73.    swap(swapword);
  74. }
  75.  
  76. ctest(c)
  77. char c;
  78. {
  79.    switch(c) {
  80.    case '"':  putchar('\'');
  81.      break;
  82.    case '{':  swap("BEGIN\0");
  83.      break;
  84.    case '}':  swap("END;\0");
  85.      break;
  86.    case '\t':  swap("  \0");
  87.      break;
  88.    case '&':  swapif('&','&',"AND  \0");
  89.      break;
  90.    case '|':  swapif('|','|',"OR  \0");
  91.      break;
  92.    case '(':  swapif('(',')',"\0");
  93.      break;
  94.    case '/':  swapif('/','*',"{\0");
  95.      break;
  96.    case '*':  swapif('*','/',"}\0");
  97.      break;
  98.    case '!':  swapif('!','=',"<>\0");
  99.      break;
  100.    case '<':
  101.    case '>':  putchar(c);      /* <x and >x are passed unchanged */
  102.               c=getchar();
  103.               putchar(c);
  104.      break;
  105.    case '=':  identassign();   /* == -> =, = -> := */
  106.      break;
  107.    default :  putchar(c);
  108.      break;
  109.  
  110.    }
  111. }
  112.  
  113. swap(s)
  114. char *s;
  115. {
  116.    while(*s != EOS) putchar(*s++);
  117. }
  118.  
  119. swapif(first, second, replacement)
  120. char first, second, *replacement;
  121. {
  122.    char c;
  123.  
  124.    if((c=getchar()) == second) swap(replacement);
  125.    else {
  126.       putchar(first);
  127.       putchar(c);
  128.    }
  129.  
  130. }
  131.  
  132. identassign()
  133. {
  134.    char c;
  135.  
  136.    if((c=getchar()) != '=') {          /* assignment */
  137.       putchar(':');
  138.       putchar('=');
  139.    }
  140.    putchar(c);
  141. }
  142.  
  143. /* end of ctp.c */
  144.